<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Lunbo;

class LunboController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         $lunbo = Lunbo::get();
         // dd($lunbo);
        //读取用户的信息
        return view('admin.lunbo.index', ['lunbo'=>$lunbo]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.lunbo.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 实例化对象
        $lunbo = new Lunbo;
        $lunbo->href = $request->href;
        $lunbo->status = $request->status;

        // 上传轮播图
        if($request->hasFile('img')) {
            //上传文件夹的路径
            $dirname = './index/'.date('Ymd').'/';
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //文件名
            $name = time().rand(100000,999999).'.'.$suffix;
            $request->file('img')->move($dirname, $name);
            //存入对象中(数据表中)
            $lunbo->img = trim($dirname,'.').$name;
        }

        if($lunbo->save()){
            return redirect('/lunbo');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        $lunbo = Lunbo::where('status','=',1)->get();

        //解析模板
        return view('layout.layout', ['lunbo'=>$lunbo]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //读取信息
        $lunbo = Lunbo::findOrFail($id);
        //显示模板
        return view('admin.lunbo.edit', ['lunbo'=>$lunbo]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 实例化对象
        $lunbo = Lunbo::findOrFail($id);
        $lunbo->href = $request->href;
        $lunbo->status = $request->status;

        // 上传轮播图
        if($request->hasFile('img')) {
            //上传文件夹的路径
            $dirname = './index/'.date('Ymd').'/';
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //文件名
            $name = time().rand(100000,999999).'.'.$suffix;
            $request->file('img')->move($dirname, $name);
            //存入对象中(数据表中)
            $lunbo->img = trim($dirname,'.').$name;
        }

        if($lunbo->save()){
            return redirect('/lunbo');
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取数据
        $lunbo = Lunbo::findOrFail($id);

        if($lunbo->delete()){
            return back()->with('success','执行成功');
        }else{
            return back()->with('error','执行失败');
        }
    }
}
